const readline = require('readline');
const net = require('net');
// const PIPE_PATH = '/tempcontainer/HmiRuntime';

module.exports = function (RED) {
  function HmiRuntimeReadTagsNode(config) {
    RED.nodes.createNode(this, config);

    // JSON object for output message of this node
    const msg = { payload: {} };

    const node = this;
    const PIPE_PATH = config.Pipe_Path;
    console.log("PIPE_PATH:", PIPE_PATH)
    const client = net.connect(PIPE_PATH, function () {
      node.log('Socket connection to Pipe Server successfully established.');

      const rl = readline.createInterface({
        input: client,
        crlfDelay: Infinity
      });
      // Unified response
      rl.on('line', (line) => {
        msg.payload = JSON.parse(line);
        node.send(msg);
      });
      //Unified request
      node.on('input', function (inputMessage) {
        // const tags = inputMessage.TagNames.split(' ');
        //  Node-red中是如何使用
        // tags={}
        // tags.TagNames="ddd aa"
        // return tags;
        const tags = config.tags.split(' ');
        const readTagsObject = { Message: 'ReadTag', Params: { Tags: tags }, ClientCookie: 'NodeRedCookieForReadTags' };
        node.log('Read for Tags: ' + tags);
        const readTagsCmd = JSON.stringify(readTagsObject) + '\n';
        client.write(readTagsCmd);
      });
    });
    client.on('end', function () {
      node.log('Socket connection has been closed. Try to reconnect.');
    });
  }
  RED.nodes.registerType('hmi-read-tags', HmiRuntimeReadTagsNode);
};
